package backEnd;

public abstract class Game {

	String type;            // Type of the game - Addition/Multiplication
	public Player player1;  // Player1 - gets instantiated in child class
	public Player player2;  // Player1 - gets instantiated in child class
	public Player moveOf;   // Sets the current move of the player
	public int won;         // If a move results in win, this variable is set
	
	String gameBoard[][];   // Current status of the game board
	String colorBoard[][];  // Current color of the game board
	public Hint hint;       // Gets instantiated in child class
	
	/**
	 * Function to initialize the game board with values and colors
	 */
	protected void initializeGameBoard(){
		gameBoard = new String[10][10];
		colorBoard = new String[10][10];
		for (int i = 1; i < 10; i++) {
			gameBoard[0][i] = i + "";
			gameBoard[i][0] = i + "";
		}
		for (int i = 1; i < 10; i++) {
			for (int j = 1; j < 10; j++) {
				gameBoard[i][j] = "?";
				colorBoard[i][j] = "none";
			}
		}
	}
	
	/**
	 * Getter function for the game board
	 * @return global value gameBoard
	 */
	public String[][] getGameBoard(){
		return gameBoard;
	}
	
	/**
	 * Sets the current move
	 * @param row
	 * @param col
	 */
	protected abstract void updateBoard(int row, int col);

	/**
	 * Updates all the parameters and checks for win
	 * Called from front end
	 * @param row
	 * @param col
	 */
	public String updateMove(int row, int col){

            // If square has already been selected
            if(!gameBoard[row][col].equals("?")){
                return "??";
            }
                //Check for valid answer for the hint
                String ans = hint.evaluate(type, row, col);

                //Invalid move
                if (ans.equals("??")){
                    return ans;
                }

		//Update Grid
		updateBoard(row,col);
		
		//Update Hint
		hint.setNextHint(type);
		
		//Update color on board
		colorBoard[row][col] = moveOf.color;
		
		//Check win lose information
		for (int i = 1; i < 10; i++) {
			if (colorBoard[i][1].equals("blue") && won == 0) {
				checkForWin(i, 1, "blue");
			}
			if (colorBoard[1][i].equals("red") && won == 0) {
				checkForWin(1, i, "red");
			} 
		}
		
		//Update current player info
		moveOf = (moveOf.equals(player1)) ? player2 : player1;

                //Return valid move
                return ans;
	}
		
	/**
	 * Utility function to check if any player has won based on the current color on the board
	 * @param row
	 * @param cell
	 * @param color
	 */
	private void checkForWin(int row, int cell, String color) {

		if (row < 1 || cell < 1 || row > 9 || cell > 9 || colorBoard[row][cell].equals(color) == false) {
			return;
		} else if (cell == 9 && color.equals("blue")&& colorBoard[row][cell].equals("blue")) {
			won = 1;			
			return;
		} else if (row == 9 && color.equals("red") && colorBoard[row][cell].equals("red")) {
			won = 2;
			return;
		} else {
			if (colorBoard[row][cell].equals("blue") && color.equals("blue")) {
				checkForWin(row - 1, cell + 1, "blue");
				checkForWin(row, cell + 1, "blue");
				checkForWin(row + 1, cell + 1, "blue");
			}
			if (colorBoard[row][cell].equals("red") && color.equals("red")) {
				checkForWin(row + 1, cell - 1, "red");
				checkForWin(row + 1, cell, "red");
				checkForWin(row + 1, cell + 1, "red");
			}
		}
	}

}
